<?php

class Empeval_Methods_Request_GetEmployeeBonusGoals {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	
	public $getAll = false;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->full = (bool)$params->full;
	}
	
	public function process() {
		$bonusGoalsFormsQuery = EmpevalViewBgfWithSuperiorPositionIdQuery::create();
		if (!$this->full) {
			$bonusGoalsFormsQuery
			->useUserPositionsRelatedBySuperiorUserPositionIdQuery('SuperiorUserPositions')
				->filterByUsers(Empeval_Auth::getInstance()->getUser())
			->endUse();
		}
		else {
			if (!Empeval_Auth::getInstance()->getUser()->isHr() &&  !Empeval_Auth::getInstance()->getUser()->isAdmin())
				throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
			$bonusGoalsFormsQuery->leftJoinUserPositionsRelatedBySuperiorUserPositionId('SuperiorUserPositions');
		}
		$bonusGoalsFormsQuery
		->useBonusPeriodsQuery(null, Criteria::LEFT_JOIN)
			->orderByNamePl(Criteria::DESC)
		->endUse()
		->useUserPositionsRelatedByUserPositionIdQuery('OwnerUserPositions')
			->joinUsers('OwnerUser')
		->endUse()
		->useUsersQuery('Editor', Criteria::LEFT_JOIN)
			->useUserRolesRelatedByToUserIdQuery('EditorUserRoles', Criteria::LEFT_JOIN)
				->leftJoinRoles('EditorRoles')
			->endUse()
			->useUserPositionsQuery('EditorUserPositions', Criteria::LEFT_JOIN)
				->leftJoinPositions('EditorPositions')
			->endUse()
			->addJoinCondition('EditorUserPositions', 'EditorUserPositions.Active = 1')
		->endUse()
		->filterByDeletedDate(null, Criteria::ISNULL)
		->with('SuperiorUserPositions')
		->with('OwnerUserPositions')
		->with('OwnerUser')
		->with('BonusPeriods')
		->with('Editor')
		->with('EditorUserRoles')
		->with('EditorRoles')
		->with('EditorUserPositions')
		->with('EditorPositions');
		$bonusGoalsForms = $bonusGoalsFormsQuery->find();
		
		$userPosition = Empeval_Auth::getInstance()->getUser()->getUserPositionss()->getFirst();
		$childUnits = array();
		foreach ($userPosition->getUnits()->getUnitssRelatedById() as $unit) {
			$childUnits[] = $unit->getId();
		}
		
		$users = EmpevalUsersQuery::create()
		->useUserPositionsQuery()
			->addSuperiorUserPositionFilters($userPosition->getUnitId(), $childUnits)
			->usePositionsQuery()
				->filterByGrade(null, Criteria::ISNOTNULL)
			->endUse()
			->filterByActive(1)
		->endUse()
		->find();
		
		$bonusPeriods = EmpevalBonusPeriodsQuery::create()->find();
		
		return new Empeval_Methods_Response_GetEmployeeBonusGoals(true, $bonusGoalsForms, $users, $bonusPeriods);
	}
	
}